package com.suneee.spec.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suneee.category.dao.CategoryDao;
import com.suneee.category.service.CategoryService;
import com.suneee.component.excel.ExcelUtil;
import com.suneee.context.page.Page;
import com.suneee.core.error.Assert;
import com.suneee.core.error.ErrorUtils;
import com.suneee.core.error.GenericException;
import com.suneee.resources.ProductError;
import com.suneee.spec.bean.SpecSearchBean;
import com.suneee.spec.dao.SpecDao;
import com.suneee.spec.entity.Spec;
import com.suneee.spec.service.SpecService;
import com.suneee.store.dao.SpuDao;
import com.suneee.web.bean.DownloadInfo;

/**
 * @author liuxh
 */
@Service("api-spec")
public class SpecServiceImpl implements SpecService {

	@Autowired
	private SpecDao specDao;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private SpuDao spuDao;
	
	@Autowired
	private CategoryDao categoryDao;

	@Override
	public void addSpec(Spec spec) {
		Assert.notNull(spec.getName(), "规格名称");
		Assert.notNull(spec.getCategoryCode(), "类目");
		if (specDao.existsName(spec.getName(),spec.getCategoryCode())) {
			throw ErrorUtils.wrap(ProductError.SPEC_NAME_DUPLICATE);
		}
		if (spec.getOrderNumber() < 0) {
			throw ErrorUtils.wrap(ProductError.ORDERNUMBER_DUPLICATE);
		}
		List<Spec> specList = specDao.getSpecByCategoryCode(spec.getCategoryCode());
		if(specList != null && specList.size() >0){
			int size = specList.size();
			if(size >= 4){
				throw ErrorUtils.wrap(ProductError.OUT_OF_SPECIFICATION_NUMBER);
			}
		}
		
		specDao.insert(spec);
	}

	@Override
	public void updateSpec(Spec spec) {
		Assert.notNull(spec.getName(), "规格名称");
		Assert.notNull(spec.getCategoryCode(), "类目");
		Spec old = this.getSpec(spec.getSpecId());
		if (!old.getName().equals(spec.getName()) && specDao.existsName(spec.getName(),spec.getCategoryCode())) {
			throw ErrorUtils.wrap(ProductError.SPEC_NAME_DUPLICATE);
		}
		if (spec.getOrderNumber() < 0) {
			throw ErrorUtils.wrap(ProductError.ORDERNUMBER_DUPLICATE);
		}
		specDao.update(spec);
	}

	@Override
	public void deleteSpec(Integer... specId) {
		Assert.notNull(specId, "规格id");
		try {
			List<Integer> idList = Arrays.asList(specId);
			List<Spec> list = specDao.select(idList);

			for (Spec spec : list) {
		
				if(spuDao.isUseSpec(spec.getSpecId())||spuDao.isUseEnterpriseSpec(spec.getSpecId())){
					throw ErrorUtils.wrap(ProductError.SPEC_DUPLICATE);
				}
			}
			specDao.delete(idList);

		} catch (GenericException e) {
			throw e;
		} catch (Exception e) {
			throw ErrorUtils.wrap(ProductError.SPEC_DUPLICATE);
		}
	}

	@Override
	public Page<Spec> findSpec(SpecSearchBean bean) {
		Page<Spec> findSpec = specDao.findSpec(bean);
		for (int i = 0; i < findSpec.getItems().size(); i++) {
			Spec spec = findSpec.getItems().get(i);
			String categoryCode = spec.getCategoryCode();
			String categoryNames = categoryService.getCategoryNames(categoryCode);
			spec.setCategoryNames(categoryNames);
		}
		return findSpec;
	}

	@Override
	public Spec getSpec(int specId) {
		Spec spec = specDao.select(specId);
		String categoryCode = spec.getCategoryCode();
		String categoryNames = categoryService.getCategoryNames(categoryCode);
		spec.setCategoryNames(categoryNames);
		return spec;
	}
	
	public static final String EXPORT_XLS = "/templates/spec.export.xls";

	/**
	 * 导出
	 */
	@Override
	public DownloadInfo exportSpecs(Integer[] ids,String name) {
		List<Spec> list = new ArrayList<>();
		if (ids == null ) {
			list = this.listSpec(name);
		}else {
			List<Integer> idList = Arrays.asList(ids);
			list = specDao.select(idList);
		}
		for(int i = 0; i < list.size(); i++){
			Spec spec = list.get(i);
			String categoryCode = spec.getCategoryCode();
			spec.setCategoryNames(categoryService.getCategoryNames(categoryCode));
		}
		DownloadInfo downloadInfo = ExcelUtil.writeExcle(EXPORT_XLS, list, ExcelUtil.replaceTimestamp("规格"));
		
		return downloadInfo;
	}
	

	@Override
	public List<Spec> listSpec(String name) {
		return specDao.listSpec(name);
	}

	@Override
	public List<Spec> getSpecByCategoryCode(String categoryCode) {
		Assert.notNull(categoryCode, "类目编码");
		return specDao.getSpecByCategoryCode(categoryCode);
	}
}
